package com.nado.iims2.blog.dao.manage;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.nado.extensions.sql.SimpleSqlMapDaoSupport;
import com.nado.iims2.blog.model.manage.Category;
import com.nado.iims2.blog.model.manage.Code;

@Repository("CodeDAO")
public class CodeDAOImpl extends SimpleSqlMapDaoSupport implements CodeDAO {
	private final static String PREFIX = "com.nado.iims2.blog.code.";
	
	/**
	 * Select category list.
	 * 
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public List<Category> selectCategoryList() throws SQLException {
		return getSqlMapClient().queryForList(PREFIX + "selectCategoryList");
	}
	
	/**
	 * Select code list.
	 * 
	 * @param catCd
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public List<Code> selectCodeList(String categoryCd) throws SQLException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("categoryCd", categoryCd);
		
		return getSqlMapClient().queryForList(PREFIX + "selectCodeList", params);
	}
	
	/**
	 * Select category list count.
	 */
	@Override
	public int selectCategoryListCount() throws SQLException {
		return (Integer) getSqlMapClient().queryForObject(PREFIX + "selectCategoryListCount");
	}
	
	/**
	 * Insert category info.
	 */
	@Override
	public int insertCategory(Category model) throws SQLException {
		return getSqlMapClient().update(PREFIX + "insertCategory", model);
	}
	
	/**
	 * Update category info.
	 */
	@Override
	public int updateCategory(Category model) throws SQLException {
		return getSqlMapClient().update(PREFIX + "updateCategory", model);
	}

	/**
	 * Insert code info.
	 */
	@Override
	public int insertCode(Code model) throws SQLException {
		return getSqlMapClient().update(PREFIX + "insertCode", model);
	}
	
	/**
	 * Update code info.
	 */
	@Override
	public int updateCode(Code model) throws SQLException {
		return getSqlMapClient().update(PREFIX + "updateCode", model);
	}
	
	/**
	 * Delete code info.
	 */
	@Override
	public int deleteCode(Code model) throws SQLException {
		return getSqlMapClient().delete(PREFIX + "deleteCode", model);
	}

	@Override
	public int selectByCategoryCd(String categoryCd) throws SQLException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("categoryCd", categoryCd);
		
		return (Integer) getSqlMapClient().queryForObject(PREFIX + "selectByCategoryCd", params);
	}

	@Override
	public int selectByCodeCd(String categoryCd, String code) throws SQLException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("categoryCd", categoryCd);
		params.put("code", code);
		
		return (Integer) getSqlMapClient().queryForObject(PREFIX + "selectByCodeCd", params);
	}
}
